package com.hwadee.springboot_lx.controller;

import com.hwadee.springboot_lx.service.DataService;
import com.hwadee.springboot_lx.service.RedisService;
import lombok.Setter;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Setter
@RestController
@RequestMapping("/restore")
public class RestoreController {


    private DataService dataService;
    private RedisService redisService;

    @PostMapping("/restoreData")
    public String restoreData(@RequestParam String type, @RequestParam String filePath) {
        try {
            if ("database".equalsIgnoreCase(type)) {
                dataService.restoreDatabase(filePath);
            } else if ("redis".equalsIgnoreCase(type)) {
                redisService.restoreRedis(filePath);
            } else if ("both".equalsIgnoreCase(type)) {
                dataService.restoreDatabase(filePath);
                redisService.restoreRedis(filePath);
            } else {
                return "Invalid restore type";
            }
            return "Restore successful";
        } catch (IOException e) {
            e.printStackTrace();
            return "Restore failed: " + e.getMessage();
        }
    }

}

